<?php
    /**
     * 
     */
    class Feed_Model extends CI_Model {
        
		function getFeeds($username, $limit, $offset){
			/*
			 * Get feeds specified the number of limit and starting from offset
			 */
			 
			$mainResults = array();
			
			//get activities of idea
			$this->db->from('followed_idea_list');
			$this->db->join('idea_activity', 'idea_activity.idea_id = followed_idea_list.idea_idea_id');
			$this->db->join('idea', 'idea_activity.idea_id = idea.idea_id');
			$this->db->where('followed_by_username', $username);
			$this->db->order_by('activity_date', "desc");
			$this->db->limit($limit, $offset);
			
			$ideaActivities = $this->db->get()->result();
			
			//append result to main result array
			foreach ($ideaActivities as $ideaActivity) {
				$mainResults[] = $ideaActivity;
			}
			
			//get activity of user
			$this->db->from('followed_user_list');
			$this->db->join('user_activity', 'user_activity.user_username = followed_user_list.user_username');
			$this->db->where('followed_by_username', $username);
			$this->db->order_by('activity_date', "desc");
			$this->db->limit($limit, $offset);
			
			$userActivities = $this->db->get()->result();
			
			//append result to main result array
			foreach ($userActivities as $userActivity){
				$mainResults[] = $userActivity;
			}
			
			//sort function with date, earliest first.
			function date_compare($a, $b) {
				$t1 = strtotime($a->activity_date);
				$t2 = strtotime($b->activity_date);
			return $t2 - $t1;
			}
			
			usort($mainResults, 'date_compare');
		
			//slice array
			return array_slice($mainResults, 0, $limit);
		}
		
        function addSubscriptionIdea($idea_id, $username){
        	/*
			 * Save subscription data to database
			 */
        	
        	//check whether user had subscribed to this idea
        	$success = $this->checkSubscriptionIdea($idea_id, $username);
			
			if($success != 0){
				//already is subscribed
				return 0;
			}else{
				//has not subscribed
				return $this->db->insert('followed_idea_list', array('idea_idea_id' => $idea_id, 'followed_by_username' => $username));				
			}
        }
		
		function checkSubscriptionIdea($idea_id, $username){
			/*
			 * Check for an existing subscription data
			 */
			$rows = $this->db->get_where('followed_idea_list', array('idea_idea_id' => $idea_id, 'followed_by_username' => $username))->result();
			return count($rows);
		}
		
		function deleteSubscriptionIdea($idea_id, $username){
			/*
			 * Delete subscription data to database
			 */
			
			//check whether user is subscribed to this idea
			$success = $this->checkSubscriptionIdea($idea_id, $username);
			
			if($success != 1){
				//is not subscribed
				return 0;
			}else{
				//is subscribed
				return $this->db->delete('followed_idea_list', array('idea_idea_id' => $idea_id, 'followed_by_username' => $username));
			}
		}
		
        function addSubscriptionUser($username, $followed_by_username){
        	/*
			 * Save subscription data to database
			 */
        	
        	//check whether user had subscribed to this user
        	$success = $this->checkSubscriptionIdea($username, $followed_by_username);
			if($success != 0){
				//already is subscribed
				return 0;
			}else{
				//is not subscribe
				return $this->db->insert('followed_user_list', array('user_username' => $username, 'followed_by_username' => $followed_by_username));				
			}
        }
		
		function checkSubscriptionUser($username, $followed_by_username){
			/*
			 * Check for an existing subscription data
			 */
			 
			$rows = $this->db->get_where('followed_user_list', array('user_username' => $username, 'followed_by_username' => $followed_by_username))->result();
			
			return count($rows);
		}
		
		function deleteSubscriptionUser($username, $followed_by_username){
			/*
			 * Delete subscription data to database
			 */
			
			//check whether user is subscribed to this user
			$success = $this->checkSubscriptionUser($username, $followed_by_username);
			if($success != 1){
				//is not subscribed
				return 0;
			}else{
				return $this->db->delete('followed_user_list', array('user_username' => $username, 'followed_by_username' => $followed_by_username));
			}
		}
		
		function trackIdeaActivity($idea_id, $context){
			$this->db->insert('idea_activity', array('idea_id' => $idea_id, 'activity_context' => $context));
		}
		
		function trackUserActivity($username, $context){
			$this->db->insert('user_activity', array('user_username' => $username, 'activity_context' => $context));
		}

		function getUserSubscriptionOfUser($username){
			return $this->db->get_where('followed_user_list', array('followed_by_username' => $username))->result();
		}
		
		function getIdeaSubscriptionOfUSer($username){
			
			$this->db->select('idea.idea_id, idea.idea_title, followed_by_username');
			$this->db->from('followed_idea_list');
			$this->db->join('idea', 'followed_idea_list.idea_idea_id = idea.idea_id');
			$this->db->where('followed_by_username', $username);
			
			return $this->db->get()->result();
		}

    }
    
?>